package com.cap.bts.crmdp.dealer.coupon.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.coupon.dao.ITicketLinePriceDao;
import com.cap.bts.crmdp.dealer.coupon.domain.TicketLinePriceModel;
import com.cap.bts.crmdp.dealer.coupon.dto.TicketLinePriceDto;
import com.cap.bts.framework.common.dao.callback.NativeQueryJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;

/**
 * 价目表
 * <p>Title: TicketLinePriceDaoImpl.java</p>
 * @author lizhiyong
 * @date 2015-10-27 下午3:59:13 
 * @version v 0.1
 */
@Repository("iTicketLinePriceDao")
public class TicketLinePriceDaoImpl extends BaseDaoImpl<TicketLinePriceModel, Long> implements
                                                                             ITicketLinePriceDao {

    private static final String WHERE_STRING = " where 1=1 ";
    
    private static final String A_TICKET_LINE_ID = "and a.ticket_line_id='";

    @Inject
    public TicketLinePriceDaoImpl(@Value("com.cap.bts.crmdp.dealer.coupon.domain.TicketLinePriceModel") Class<TicketLinePriceModel> clazz) {
        super(clazz);

    }

    /**
     * 根据提货券行ID查询价目表信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLinePriceDao#getMealPriceListByPackageId(com.cap.bts.crmdp.dealer.coupon.dto.TicketLinePriceDto)
     */
    @Override
    public List<TicketLinePriceModel> getMealPriceListByPackageId(TicketLinePriceDto ticketLinePriceDto)
                                                                                      throws AppException {
        String selectStr = "select a.id,a.ticket_line_id,a.code_type,case when a.code is not null then c.code_value else null end code,"
                           + "a.value,a.price,a.create_by,a.create_date,a.last_update_by,a.last_update_date from tm_raw_package_price a";

        String leftStr = " left join tc_code_value c on a.code_type=c.code_type and a.code=c.code";
        // 查询结果集的SQL
        StringBuilder selectSql = new StringBuilder();

        StringBuilder where = new StringBuilder();
        where.append(WHERE_STRING);
        if (ticketLinePriceDto.getTicketLineId() != null) {
            where.append(A_TICKET_LINE_ID + ticketLinePriceDto.getTicketLineId() + "'");
        }
        String order = " order by a.id desc";

        selectSql.append(selectStr).append(leftStr).append(where.toString()).append(order);

        return super.getJpaTemplate().execute(
            new NativeQueryJpaCallback<TicketLinePriceModel>(selectSql.toString(), TicketLinePriceModel.class));

    }

    /**
     * 根据参数查询价目表信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLinePriceDao#getMealPriceInfo(com.cap.bts.crmdp.dealer.coupon.dto.TicketLinePriceDto)
     */
    @Override
    public TicketLinePriceModel getMealPriceInfo(TicketLinePriceDto dto) throws AppException {
        String selectStr = "select a.id,a.ticket_line_id,a.code_type,a.code,"
                           + "a.value,a.price,a.create_by,a.create_date,a.last_update_by,a.last_update_date from tm_raw_package_price a";

        String leftStr = " left join tc_code_value c on a.code_type=c.code_type and a.code=c.code";
        // 查询结果集的SQL
        StringBuilder selectSql = new StringBuilder();

        StringBuilder where = new StringBuilder();
        where.append(WHERE_STRING);
        if (dto.getId() != null) {
            where.append("and a.id='" + dto.getId() + "'");
        }
        if (dto.getCode() != null) {
            where.append("and a.code='" + dto.getCode() + "'");
        }
        if (dto.getValue() != null) {
            where.append("and a.value='" + dto.getValue() + "'");
        }
        if (dto.getTicketLineId() != null) {
            where.append(A_TICKET_LINE_ID + dto.getTicketLineId() + "'");
        }

        selectSql.append(selectStr).append(leftStr).append(where.toString());
        List<TicketLinePriceModel> list = super.getJpaTemplate().execute(
            new NativeQueryJpaCallback<TicketLinePriceModel>(selectSql.toString(), TicketLinePriceModel.class));
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据提货券行ID查询价目表信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLinePriceDao#getMealPriceByPackageId(com.cap.bts.crmdp.dealer.coupon.dto.TicketLinePriceDto)
     */
    @Override
    public List<TicketLinePriceModel> getMealPriceByPackageId(TicketLinePriceDto dto) throws AppException {

        String selectStr = "select * from tm_raw_package_price a";
        // 查询结果集的SQL
        StringBuilder selectSql = new StringBuilder();

        StringBuilder where = new StringBuilder();
        where.append(WHERE_STRING);
        if (dto.getTicketLineId() != null) {
            where.append(A_TICKET_LINE_ID + dto.getTicketLineId() + "'");
        }

        selectSql.append(selectStr).append(where.toString());
        return super.getJpaTemplate().execute(
            new NativeQueryJpaCallback<TicketLinePriceModel>(selectSql.toString(), TicketLinePriceModel.class));
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年11月3日
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLinePriceDao#getAllTicketLine()
    **/
    @Override
    public List<TicketLinePriceModel> getAllTicketLine() throws AppException {
        String sql = "select * from tm_raw_package_price";
        return this.getListByNativeQuery(sql);
    }

}
